/**
 * 
 */
package paymentLogic;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;

import constants.Constants;
import encryption.Encrypt;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextField;
import paymentEntity.CashEntity;
import paymentEntity.CreditCardEntity;
import paymentService.PaymentService;
import userEntity.MembershipEntity;
import userLogic.MembershipLogic;
import validator.Validator;

/**
 * @author Yokesh Kumar
 *
 */
public class PaymentLogic {

	/**
	 * check whether the credit card value entered by the user is valid
	 * @param CreditCard
	 * @return
	 */
	public boolean validateCreditCard(TextField creditCardNo1,TextField creditCardNo2,
							TextField creditCardNo3,TextField creditCardNo4){

		if(creditCardNo1!= null && !creditCardNo1.getText().isEmpty() &&
				creditCardNo2!= null && !creditCardNo2.getText().isEmpty() &&
				creditCardNo3!= null && !creditCardNo3.getText().isEmpty() &&
				creditCardNo4!= null && !creditCardNo4.getText().isEmpty()){
			if(Validator.validateStringContainsInteger(creditCardNo1.getText().trim()) && 
					Validator.validateStringContainsInteger(creditCardNo2.getText().trim()) &&
					Validator.validateStringContainsInteger(creditCardNo3.getText().trim()) &&
					Validator.validateStringContainsInteger(creditCardNo4.getText().trim())){
					return true;
			}
			else{
				return false;
			}
		}
		else{
			
			return false;
		}
	}

	/**
	 * check whether the expiry date value entered by the user is valid
	 * @param expiry
	 * @return
	 */
	public boolean validateExpiry(TextField expiryMonth, TextField expiryYear){
		if(expiryMonth!=null && !expiryMonth.getText().isEmpty() &&
				expiryYear!=null && !expiryYear.getText().isEmpty() ){
			if(Validator.validateStringContainsInteger(expiryMonth.getText().trim()) && 
					Validator.validateStringContainsInteger(expiryYear.getText().trim())){
			
				Integer year = new Integer(expiryYear.getText().trim());
				Integer month = new Integer(expiryMonth.getText().trim());
				Calendar cal = Calendar.getInstance();
				Integer currYear = cal.get(Calendar.YEAR);
				Integer currmonth = cal.get(Calendar.MONTH);
				String Yr = currYear.toString();
				currYear = new Integer(Yr.substring(2, 4));
				if(currYear > year){
					return false;
				}
				else if(month > 12){
					return false;
				}
				else if(year == currYear &&  currmonth > month){
					return false;
				}
				else{
					return true;
				}
			}
			else{
				return false;
			}
		}
		else{
			return false;
		}
	}

	/**
	 * @param creditCardNo1
	 * @param creditCardNo2
	 * @param creditCardNo3
	 * @param creditCardNo4
	 * @param expiryMonth
	 * @param expiryYear
	 * @param cardType
	 */
	public String makeCreditcardPaymentForMembership(TextField creditCardNo1,
			TextField creditCardNo2, TextField creditCardNo3,
			TextField creditCardNo4, TextField expiryMonth,
			TextField expiryYear, ChoiceBox<String> cardType) {

		CreditCardEntity creditCard = new CreditCardEntity();
		int paymentId = 0;
		int membershipId = 0;
		String cardNo = "";
		String expiryDate = "";
		String card = "";
		String message = "";
		
		if(validateCreditCard(creditCardNo1, creditCardNo2, creditCardNo3, creditCardNo4)){
			cardNo = creditCardNo1.getText().trim().concat(creditCardNo2.getText().trim()).concat(creditCardNo3.getText().trim()).concat(creditCardNo4.getText().trim());
		}
		else{
			return "Please enter a valid credit card number";
		}
		if(validateExpiry(expiryMonth, expiryYear)){
			expiryDate = expiryMonth.getText().trim().concat(expiryYear.getText().trim());
		}
		else{
			return "Expiry date is not valid or your card has already expired";
		}
		
		if(cardType != null && !cardType.getValue().isEmpty() && !cardType.getValue().endsWith("--Select--")){
			card = cardType.getValue();
		}
		else{
			message = "please select card type";
			return message;
		}
		
		PaymentService paymentService = new PaymentService();
		try{
			creditCard.setCardNo(Encrypt.encrypt(cardNo));
			creditCard.setCardType(card);
			creditCard.setExpiryDate(new Integer(expiryDate));
			creditCard.setPaidFor(Constants.NEW_MEMBERSHIP);
			creditCard.setPaymentAmount(Constants.NEW_MEMBERSHIP_AMOUNT);
			creditCard.setPaymentType(Constants.CREDIT_CARD);
			ResultSet rs = paymentService.insertIntoPayment(creditCard);
			if(rs != null && rs.next()){
				paymentId = rs.getInt("last_id");
				if(paymentId != 0){
					System.out.println("Payment iD : " + paymentId);
					creditCard.setPaymentId(paymentId);
					paymentService.insertIntoCreditCard(creditCard);
					paymentService.transactionCommit();
				}
				else{
					paymentService.transactionRollBack();
				}
			}
			else{
				System.out.println("payment id not returned");
			}
			
			if(paymentId != 0){
				MembershipLogic membership = new MembershipLogic();
				membershipId = membership.createMembership(paymentId);
				if(membershipId != 0){
					message = "Payment successfull with Payment ID " + paymentId + " and membershipID " + membershipId ;
				}
				else{
					paymentService.transactionRollBack();
				}
			}
		}
		catch(SQLException sqlExec){
			sqlExec.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		paymentService.closeConnection();
		return message;
	}

	public String makeCreditcardPaymentForMembershipRenewal(TextField creditCardNo1,
															TextField creditCardNo2, TextField creditCardNo3,
															TextField creditCardNo4, TextField expiryMonth,
															TextField expiryYear, ChoiceBox<String> cardType) {

		CreditCardEntity creditCard = new CreditCardEntity();
		int paymentId = 0;
		int membershipId = 0;
		String cardNo = "";
		String expiryDate = "";
		String card = "";
		String message = "";
		
		if(validateCreditCard(creditCardNo1, creditCardNo2, creditCardNo3, creditCardNo4)){
			cardNo = creditCardNo1.getText().trim().concat(creditCardNo2.getText().trim()).concat(creditCardNo3.getText().trim()).concat(creditCardNo4.getText().trim());
		}
		else{
			return "Please enter a valid credit card number";
		}
		
		if(validateExpiry(expiryMonth, expiryYear)){
			expiryDate = expiryMonth.getText().trim().concat(expiryYear.getText().trim());
		}
		else{
			return "Expiry date is not valid or your card has already expired";
		}
		if(cardType != null && !cardType.getValue().isEmpty() && !cardType.getValue().endsWith("--Select--")){
			card = cardType.getValue();
		}
		else{
			message = "please select card type";
			return message;
		}
		
		PaymentService paymentService = new PaymentService();
		try{
			creditCard.setCardNo(Encrypt.encrypt(cardNo));
			creditCard.setCardType(card);
			creditCard.setExpiryDate(new Integer(expiryDate));
			creditCard.setPaidFor(Constants.MEMBERSHIP_RENEW);
			creditCard.setPaymentAmount(Constants.RENEW_MEMBERSHIP_AMOUNT);
			creditCard.setPaymentType(Constants.CREDIT_CARD);
			ResultSet rs = paymentService.insertIntoPayment(creditCard);
			if(rs != null && rs.next()){
				paymentId = rs.getInt("last_id");
				if(paymentId != 0){
					System.out.println("Payment iD : " + paymentId);
					creditCard.setPaymentId(paymentId);
					paymentService.insertIntoCreditCard(creditCard);
					paymentService.transactionCommit();
				}
			}
			else{
				System.out.println("payment id not returned");
			}
			
			if(paymentId != 0){
				MembershipLogic membership = new MembershipLogic();
				int renewalID = membership.renewMembership(paymentId);
				if(renewalID!= 0){
					message = "Renewal successfull with Payment ID " + paymentId + " and renewal ID " + renewalID;
				}
				else{
					paymentService.transactionRollBack();
				}
			}
			else{
				System.out.println("renewal id not returned");
			}
		}
		catch(SQLException sqlExec){
			paymentService.closeConnection();
			sqlExec.printStackTrace();
			
		} catch (Exception e) {
			paymentService.closeConnection();
			e.printStackTrace();
		}
		paymentService.closeConnection();
		return message;
	}
	/**
	 * Stores the Payment details of the payment made by cash
	 * returns payment id
	 * @param cash
	 */
	public int makeCashPayment(CashEntity cash){

		return 0;
	}
}
